<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
ob_start("ob_gzhandler");
require('../includes/init.php');
require ('./configs/site_config.php');
require('./classes/control/control_product_attributes.php');
require('./classes/control/control_template.php');
class ProductAttributes 
{
	var $product_attributes_c;
	var $template_c;
	var $webType;//网站类型
	function __construct(){
	
		$this->product_attributes_c = new ProductAttributesControl();
		$this->template_c = new TemplateControl();
		$this->webType = empty($_REQUEST['web_type']) ? 1 : (int) trim($_REQUEST['web_type']);
	}
	//Attributes管理
	function index()
	{
		if($_GET['in_jax'] == 1)
		{
			$type = $_GET['type'];
			$value = trim($_GET['value']);
			switch($type) 
			{
				case 'attributes_delete':
					$field = 'is_delete';
					break;
				case 'attributes_name':
					$field = 'name';
					$this->updateTemplate((int) $_GET['attributes_id'], 'ajax');
					break;
			}
			if (!empty($_GET['attributes_id'])){
				echo $this->product_attributes_c->change_info((int)$_GET['attributes_id'],$value,$field);
			}
		}	
		elseif($_REQUEST['act'] == 'add' || $_REQUEST['act'] == 'edit')//修改
		{
			if($_POST['sub_from'])
			{
				if(!empty($_POST['attributes_id']))
				{
					$affect = $this->product_attributes_c->update_products_attributes((int)$_POST['attributes_id'],$_POST['attributes_name']);
					if($affect == 1)
					{
						echo  '<script type="text/javascript"> alert("修改成功！");
							   </script>';
						
						$this->updateTemplate((int) $_POST['attributes_id']);
					}
				}else
				{
					$affect = $this->product_attributes_c->update_products_attributes(0,$_POST['attributes_name'],$this->webType);
					if($affect == 1)
					{
						echo  '<script type="text/javascript"> alert("增加成功！");</script>';
					}
				}
			}
			
			if($_GET['attributes_id'])
			{
				$products_attributes_info = array();
				$products_attributes_info = $this->product_attributes_c->get_products_attributes_info('edit',(int)$_GET['attributes_id']);
				$GLOBALS['smarty']->assign('tile',     '修改');
				$GLOBALS['smarty']->assign('form_act',     'edit');
				$GLOBALS['smarty']->assign('attributes_info',$products_attributes_info[0] );
			}else
			{
				$GLOBALS['smarty']->assign('tile',     '增加');
				$GLOBALS['smarty']->assign('form_act',     'add');
			}
			$GLOBALS['smarty']->assign('web_type',     $this->webType);
			$GLOBALS['smarty']->assign('web_info',   get_web_info($this->webType));
			$GLOBALS['smarty']->display('product_attributes_info.htm'); 
		}
		else
		{
			$pageSize = 20;//定义每页显示20条记录
			$url_info = $products_attributes_info = array();
			//分页处理
			$page = 1;
			
			if (!empty($_REQUEST['page'])) {
				$page = (int)$_REQUEST['page'];
			}
			
			$url_info = $this->product_attributes_c->get_url_info($page,$pageSize,$this->webType);
			$products_attributes_info = $this->product_attributes_c->get_products_attributes_info('list',0,$url_info['page_info']['start'],$pageSize,$this->webType);
			
			$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
			$GLOBALS['smarty']->assign('attributes_info',$products_attributes_info );
			$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
			$GLOBALS['smarty']->assign('web_type',     $this->webType);
			$GLOBALS['smarty']->assign('web_info',   get_web_info($this->webType));
			$GLOBALS['smarty']->display('product_attributes_index.htm'); 
		}	
	}
	
	/**
	* 修改与发布模板操作
	* @param   int      $attributes_id  产品属性ID
	* @param   int      $w_type   网站标识
	
	* @return  string
	*/
	function updateTemplate($attributes_id = 0, $type = '') {
		
		//定义更新分类模板
		$update_arr = array('id' => 0,
							'w_type' => (int) $this->webType,
							'operation_type' => 'product',
							'type' => 'update',
							
		);
		
		$sql = "SELECT id FROM won_product 
				WHERE FIND_IN_SET('{$attributes_id}', won_attributes_id) AND won_product_delete = 0 AND web_type = '{$this->webType}' ";
		
		$result = $GLOBALS['db']->query($sql);
	
		while ($row = $GLOBALS['db']->fetch_array($result)) {
			
			$update_arr['id'] = $row['id'];
		
		    $this->template_c->updateTemplate($update_arr);
		}
	
		if ($type != 'ajax') {
			
			echo '<script type="text/javascript">
					window.location.href="/wondershare/product_attributes.php?action=index&web_type='.$this->webType.'";		
				  </script>';
		}
		
	}


}
$attributes    = $_REQUEST['action']; 
$attributes   = new ProductAttributes;
if(method_exists($attributes, $action)) 
{
	$attributes->$action();
}else{
	$attributes->index();
}
	
?>
